<?php
namespace api\zmt\middleware;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class CheckToken
{
    public function handle($request , \Closure $next){
        $header = request()->header();
        if (!isset($header['token']) || empty($header['token'])){
            return json(['code'=> 0 ,'msg'=>'缺少 token !']);
        }
        // halt(config('app.Jwt_key'));
        $key = new Key(config('app.Jwt_key'), 'HS256');
        try{
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception){
            return json(['code'=> 0 ,'msg'=>$exception->getMessage()]);
        }
        $request->userId = $res->data->user_id;
        return $next($request);
    }
}